package cn.lili.modules.promotion.serviceimpl;

import cn.lili.common.vo.PageVO;
import cn.lili.modules.promotion.entity.dos.GreenCount;
import cn.lili.modules.promotion.entity.dos.GreenPool;
import cn.lili.modules.promotion.entity.dos.GreenUnitPrice;
import cn.lili.modules.promotion.entity.vos.GreenUnitPriceVO;
import cn.lili.modules.promotion.mapper.GreenCountMapper;
import cn.lili.modules.promotion.mapper.GreenUnitPriceMapper;
import cn.lili.modules.promotion.service.GreenPoolService;
import cn.lili.modules.promotion.service.GreenUnitPriceService;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.annotations.ApiModelProperty;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

/**
 * @author xiangqian
 * @version 1.0
 * @data 2023/12/20 13:46
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class GreenUnitPriceServiceImpl extends ServiceImpl<GreenUnitPriceMapper, GreenUnitPrice> implements GreenUnitPriceService {

    private final GreenPoolService greenPoolService;
    @Resource
    private final GreenCountMapper greenCountMapper;

    /**
     * 查询当前最新单价
     * */
    @Override
    public BigDecimal getNewUnitPrice() {
        LambdaQueryWrapper<GreenUnitPrice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(GreenUnitPrice::getCreateTime);
        queryWrapper.last("limit 1");
        GreenUnitPrice greenUnitPrice = this.getOne(queryWrapper);
        return greenUnitPrice == null ? new BigDecimal(0) : greenUnitPrice.getUnitPrice();
    }

    /**
     * 计算单价
     *
     * @return 返回结果
     */
    @Override
    public Boolean initGreenUnitPrice() {
        //查询绿色积分池数据
        GreenPool greenPool = greenPoolService.getGreenPool();
        if (greenPool == null) {
            return false;
        }
        //查询贡献值总量
        BigDecimal contributionAllValue = greenCountMapper.getContributionAllValue();
        if (contributionAllValue == null) {
            return false;
        }

        //计算单价
        BigDecimal unitPrice = greenPool.getGreenPool().divide(contributionAllValue,10, RoundingMode.HALF_UP);

        GreenUnitPrice greenUnitPrice = new GreenUnitPrice();
        greenUnitPrice.setUnitPrice(unitPrice);
        greenUnitPrice.setUnitTime(new Date());
        return this.save(greenUnitPrice);
    }

    /**
     * 分页查询单价列表
     *
     * @param greenUnitPrice 查询条件{@link GreenUnitPrice}
     * @param page           分页条件{@link PageVO}
     * @return 分页返回结果
     */
    @Override
    public Page<GreenUnitPrice> greenUnitPricePage(GreenUnitPrice greenUnitPrice, PageVO page) {
        return this.baseMapper.selectPage(PageUtil.initPage(page), greenUnitPrice.queryWrapperPage());
    }

    /**
     * 分页查询单价折线图
     *
     * @param greenUnitPrice 查询条件{@link GreenUnitPrice}
     * @param page           分页条件{@link PageVO}
     * @return 分页返回结果
     */
    @Override
    public List<GreenUnitPriceVO> greenUnitPricePicturePage(String type) {
        List<GreenUnitPriceVO> resultList;
        switch (type){
            case "0":
                //按日查询
                resultList = this.baseMapper.queryDay();
                break;
            case "1":
                //按本周查询
                resultList = this.baseMapper.queryWeeks();
                break;
            case "2":
                //按本月查询
                resultList = this.baseMapper.queryMonth();
                break;
            default:
                resultList = this.baseMapper.queryDay();
                break;
        }
        return resultList;
    }
}
